// import { queryContent, removeContent, addContent } from '../services/api';
import { fetchContentList, addContent, upGoodsList, downGoodsList, delGoodsList, fetchChannelTree, fetchTagList, fetchAuthorList, addBooklet, fetchGoodsDetail, editContent, editBooklet } from '../services/contentService';
import { decryptRes, dealPagination } from '../utils/requestUtil';
import { message } from 'antd';
export default {
  namespace: 'content',

  state: {
    condition: 1,
    goodsId: null,

    goodsDetail: null,

    contentList: [],
    pagination: { pageNum: 1, pageSize: 10 },

    channelTree: [],
    tagList: [],
    authorList: [],
    
  },

  effects: {
    *fetch({ payload, condition }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(fetchContentList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'contentListReducer',
          payload: { data },
          // condition: condition,
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchGoodsDetail({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(fetchGoodsDetail, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'goodsDetailReducer',
          payload: { data },
        });
        if (callback) { callback(data) }
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *addContent({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(addContent, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;

        message.success('添加成功！')
        console.log('添加后返回的数据：', data.goodsDetail);

        if (callback) callback(data.goodsDetail);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }

    },

    *editContent({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(editContent, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;

        message.success('修改成功！')
        console.log('添加后返回的数据：', data.goodsDetail);

        if (callback) callback(data.goodsDetail);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }

    },

    *addBooklet({ payload, callback }, { call, put }) {
      console.log('addBooklet前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(addBooklet, payload)));
      console.log('addBooklet接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;

        message.success('添加成功！')
        console.log('添加后返回的booklet数据：', data.contentBooklet);

        if (callback) callback(data.contentBooklet);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *editBooklet({ payload, callback }, { call, put }) {
      console.log('editBooklet', payload)
      const response = JSON.parse(decryptRes(yield call(editBooklet, payload)));
      console.log('editBooklet接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;

        message.success('修改成功！')
        console.log('editBooklet后返回的booklet数据：', data.contentBooklet);

        if (callback) callback(data.contentBooklet);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },


    *upGoodsList({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(upGoodsList, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('上架成功！')
        // yield put({
        //   type: 'addContentReducer',
        //   payload: { data },
        // });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
      if (callback) callback();
    },

    *downGoodsList({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(downGoodsList, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('下架成功！')
        // yield put({
        //   type: 'addContentReducer',
        //   payload: { data },
        // });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
      if (callback) callback();
    },

    *delGoodsList({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(delGoodsList, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('删除成功！')
        // yield put({
        //   type: 'addContentReducer',
        //   payload: { data },
        // });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
      if (callback) callback();
    },

    *fetchChannelTree({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(fetchChannelTree, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        let channelTree = [];
        let one = null;

        for (let i = 0; i < data.length; i++) {
          one = {};
          one["title"] = (data[i].appType == '1' ? '小萌早教' :
            data[i].appType == '2' ? '小萌绘本' : data[i].appType == '3' ? '萌萌乐视频' :
              data[i].appType == '4' ? '萌萌乐儿歌' :
                '其他');
          one['key'] = '' + data[i].appType;
          one['value'] = '' + data[i].appType;
          one['disabled'] = true;

          if (data[i].channelList && (data[i].channelList.length > 0)) {
            let children = [];
            for (let j = 0; j < data[i].channelList.length; j++) {
              children.push({ "title": data[i].channelList[j].channelName, "key": '' + data[i].channelList[j].channelId, "value": '' + data[i].channelList[j].channelId });
            }
            if (children && children.length > 0) {
              one['children'] = children;
            }
          }
          if (one) {
            channelTree.push(one)
          }
        }

        yield put({
          type: 'channelTreeReducer',
          payload: { data: channelTree },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchTagList({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(fetchTagList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'tagReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchAuthorList({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(fetchAuthorList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'authorListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

  },

  reducers: {
    contentListReducer(state, action) {
      console.log('state--------------------->>>>>>',action.payload.condition)
      return {
        ...state,
        contentList: action.payload.data.dataList,
        pagination: dealPagination(action.payload.data.pageInfo),
        condition: action.condition,
      };
    },
    goodsDetailReducer(state, action) {
      return {
        ...state,
        goodsDetail: action.payload.data,
      };
    },
    channelTreeReducer(state, action) {
      console.log('channelTree-----', action.payload.data)
      return {
        ...state,
        channelTree: action.payload.data,
      };
    },
    authorListReducer(state, action) {
      return {
        ...state,
        authorList: action.payload.data.dataList,
      };
    },
    tagReducer(state, action) {
      return {
        ...state,
        tagList: action.payload.data,
      };
    },
    save(state, action) {
      return {
        ...state,
        goodsId: action.payload.data.goodsId,
      };
    },
  },
};
